package mt.demo.web.controller.user;

import mt.demo.domain.user.UserDomain;
import mt.demo.domain.user.UserRoleDomain;
import mt.demo.service.comm.YomsException;
import mt.demo.service.user.RoleService;
import mt.demo.service.user.UserService;
import mt.demo.web.comm.resolverArg.WatArrayParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by watson on 2015/12/16.
 */
@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserService userService;

    //访问角色访问页面
    @RequestMapping(value = "roleFind.html", method = RequestMethod.GET)
    public ModelAndView showRoleAndUserPage()throws YomsException{
        UserRoleDomain userRoleDomain=new UserRoleDomain();
        UserDomain userDomain=new UserDomain();
        List<UserRoleDomain> roleDomains=roleService.getAllRoleDomain(userRoleDomain);
        List<UserDomain> userDomains=userService.getAllUser(userDomain);
        ModelAndView mav=new ModelAndView("user/roleShow");
        mav.addObject("roleDomains",roleDomains);
        mav.addObject("userDomains",userDomains);
        return mav;
    }

    //分配角色
    @RequestMapping(value = "allotRoleToUser.html",method = RequestMethod.GET)
    @ResponseBody
    public void allotRoleToUser(@WatArrayParam List<Integer> userIds, long roleValue)throws YomsException{
        if (userIds.size()==0||roleValue==0){
            throw new YomsException("传入的参数不能为空");
        }else {
            roleService.allotUserRole(userIds,roleValue);
        }
    }
    //取消角色
    @RequestMapping(value = "cancelRoleToUser.html",method = RequestMethod.GET)
    @ResponseBody
    public void cancelRoleToUser(@WatArrayParam List<Integer> userIds, long roleValue)throws YomsException{
        if (userIds.size()==0||roleValue==0){
            throw new YomsException("传入的参数不能为空");
        }else {
            roleService.cancelUserRole(userIds,roleValue);
        }
    }
}
